<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Laracasts\Presenter\PresentableTrait;

class Order extends Model
{
    use PresentableTrait;
    protected $presenter = \App\Presenters\OrderPresenter::class;

    protected $dates = [
        'signed_at',     // 签单时间
        'expired_at',    // 到期时间
        'actualed_at',   // 实际开始时间
        'designed_at',   // 设计完成时间
        'produced_at',   // 制作完成时间
        'finished_at'    // 上线时间
    ];

    /**
     * The table associated with the model.
     *
     * @var string
     */
	protected $table = 'orders';

    protected $casts = [
        'city' => 'collection'
    ];

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
	protected $guarded  = [];

    // 客户信息
    public function customer()
    {
        return $this->belongsTo('App\Models\Customer', 'cid');
    }

    // 设计师
    public function designer()
    {
        return $this->belongsTo('App\Models\User', 'designer_id');
    } 

    // 录入员
    public function inputer()
    {
        return $this->belongsTo('App\Models\User', 'inputer_id');
    }   

    // 制作人员
    public function programmer()
    {
        return $this->belongsTo('App\Models\User', 'programmer_id');
    }

    // 制作人员
    public function saler()
    {
        return $this->belongsTo('App\Models\User', 'saler_id');
    }

    // 订单续费
    public function renews()
    {
        return $this->hasMany('App\Models\Renew');
    }   

    // 评论回复
    public function replies()
    {
        return $this->hasMany('App\Models\Reply');
    }    

    // 登录权限
    public function webPermission()
    {
        return $this->hasOne('App\Models\WebPermission', 'order_id');   
    }

    // 语言版本
    public function lan()
    {
        return $this->hasMany('App\Models\LanComplete');
    }

    // 合同
    public function contract()
    {
        return $this->hasOne('App\Models\Contract', 'contract_no', 'contract_no'); 
    }

    public function setProgressAttribute($value)
    {
        if ($value >= 6) {
            $this->attributes['status'] = 3; 
        } elseif ($value == 1) {
            $this->attributes['status'] = 1; 
        } elseif ($value < 6)  {
            $this->attributes['status'] = 2; 
        }

        $this->attributes['progress'] = $value;
    } 

    public function setDomainAttribute($value)
    {
        $this->attributes['domain'] = str_replace('http://', '', $value);
    } 

    public function setTempUrlAttribute($value)
    {
        $this->attributes['temp_url'] = str_replace(['http://', 'https://'], '', $value);
    }

    public function setReferenceSiteAttribute($value)
    {
        $this->attributes['reference_site'] = str_replace(['http://', 'https://'], '', $value);
    }
}
